public class TestDemo2 {
    public static void main(String[] args) {
        StringBuilder stringBuilder = new StringBuilder("Hello World");
        //删除[1,3)区间的字符串
//        stringBuilder.delete(1,3);
//        System.out.println(stringBuilder);
        //在下标为2的位置插入字符或字符串
//        stringBuilder.insert(2,'x');
//        stringBuilder.insert(2,"xxx");
//        System.out.println(stringBuilder);
        //删除下标为2的字符
//        stringBuilder.deleteCharAt(2);
//        System.out.println(stringBuilder);
        //删除[1,3)区间字符
//        stringBuilder.delete(1,3);
//        System.out.println(stringBuilder);
        //修改对象存储空间的大小
//        System.out.println(stringBuilder.capacity());
//        stringBuilder.setLength(100);
//        System.out.println(stringBuilder.capacity());
        //字符串逆序
        System.out.println(stringBuilder);
        stringBuilder.reverse();
        System.out.println(stringBuilder);

    }
    public static void main3(String[] args) {
        //重新创建一个对象以实现字符串叠加的目的
        String str1 = new String("hello ");
        str1 += "world";
        System.out.println(str1);
        //直接修改对象的内容
        StringBuilder str2 = new StringBuilder("hello ");
        str2.append("world");
        System.out.println(str2);
    }

    public static void main2(String[] args) {
        //创建新的对象
        String str1 = new String("");
        long startTime1 = System.currentTimeMillis();
        //在写较大数字时可以加下划线来增加可读性
        for (int i = 1; i <= 1_0000; i++) {
            str1 += i;
        }
        long endTime1 = System.currentTimeMillis();
        System.out.println("时间："+(endTime1 - startTime1));

//        修改原来的对象内容，这会使得代码运行效率大大提高
        StringBuilder str2 = new StringBuilder("");
        long startTime2 = System.currentTimeMillis();
        for (int i = 1; i <1_0000 ; i++) {
            str2.append(i);
        }
        long endTime2 = System.currentTimeMillis();
        System.out.println("时间："+(endTime2 - startTime2));
    }

//    判断字符串是否为空
    public static void main1(String[] args) {

        String str1 = new String("hello");
        String str2 = new String();
        System.out.println(str1.isEmpty());
        System.out.println(str2.isEmpty());
    }
}
